// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#include <gtest/gtest.h>
#include <pollux/dwio/dwrf/utils/proto_utils.h>
#include <pollux/type/hive/hive_type_parser.h>
#include <pollux/type/hive/hive_type_serializer.h>

using namespace kumo::pollux::dwrf;
using namespace kumo::pollux::type::hive;

TEST(ProtoUtilsTests, AllTypes) {
  std::vector<std::string> types{
      "struct<a:boolean,b:tinyint,c:smallint,d:int,e:bigint,f:float,g:double,f:string,g:binary,h:timestamp>",
      "struct<a:map<int,array<struct<a:map<string,int>,b:array<int>>>>>"};

  for (auto& type : types) {
    HiveTypeParser parser;
    auto schema = parser.parse(type);
    proto::Footer footer;
    ProtoUtils::writeType(*schema, footer);

    auto out = ProtoUtils::fromFooter(footer);
    auto str = HiveTypeSerializer::serialize(out);

    EXPECT_EQ(str, type);
  }
}

TEST(ProtoUtilsTests, Projection) {
  HiveTypeParser parser;
  auto schema = parser.parse(
      "struct<a:boolean,b:tinyint,c:smallint,d:struct<a:int,b:int,c:int>>");
  proto::Footer footer;
  ProtoUtils::writeType(*schema, footer);

  auto type = ProtoUtils::fromFooter(
      footer, [](auto id) { return id != 2 && id != 5; });
  auto res = HiveTypeSerializer::serialize(type);

  EXPECT_EQ("struct<a:boolean,c:smallint,d:struct<b:int,c:int>>", res);
}
